package com.guet.news_main.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guet.common.utils.R;
import com.guet.news_main.entity.NewsArticle;
import com.guet.news_main.service.NewsArticleService;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guet
 * @since 2021-12-10
 */
@RestController
@RequestMapping("/news_main/news-article")
@CrossOrigin
public class NewsArticleController {
    @Autowired
    private NewsArticleService service;
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @PostMapping("kindArticle/{kind}")
    public R kindArticle(@PathVariable String kind){
        QueryWrapper<NewsArticle> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("kind",kind);
        List<NewsArticle> list = service.list(queryWrapper);
        return R.ok().data("rows",list);
    }

    @PostMapping("kindPageArticle/{kind}/{page}/{limit}")
    public R KindPageArticle(@PathVariable String kind,@PathVariable Long page,@PathVariable Long limit){

        Page<NewsArticle> pages=new Page<>(page,limit);
        QueryWrapper<NewsArticle> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("kind",kind);
        IPage<NewsArticle> page1 = service.page(pages,queryWrapper);
        List<NewsArticle> records = page1.getRecords();
        long total = page1.getTotal();
        return R.ok().data("rows",records).data("total",total);
    }

    @PostMapping("addArticle")
    public R addArticle(@RequestBody NewsArticle article) throws IOException {
        boolean save = service.save(article);

        //获取索引请求
        IndexRequest indexRequest=new IndexRequest("news");
        indexRequest.id(article.getId());
        //数据装载
        indexRequest.source(JSON.toJSONString(article), XContentType.JSON);
        //发送请求
        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        return save?R.ok():R.error();
    }

    @DeleteMapping("deleteArticle/{id}")
    public R deleteArticle(@PathVariable String id) throws IOException {
        boolean b = service.removeById(id);
        DeleteRequest deleteRequest=new DeleteRequest("news",id);
        restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);
        return b?R.ok():R.error();
    }

    @GetMapping("getArticleById/{id}")
    public R getArticleById(@PathVariable String id){
        NewsArticle byId = service.getById(id);
        return R.ok().data("article",byId);
    }

    @PostMapping("pageArticle/{page}/{limit}")
    public R pageArticle(@PathVariable Long page,@PathVariable Long limit){

        Page<NewsArticle> pages=new Page<>(page,limit);
        IPage<NewsArticle> page1 = service.page(pages,null);
        List<NewsArticle> records = page1.getRecords();
        long total = page1.getTotal();
        return R.ok().data("rows",records).data("total",total);
    }

    @PostMapping("updateArticle")
    public R updateArticle(@RequestBody NewsArticle article){
        QueryWrapper<NewsArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("id",article.getId());
        boolean update = service.update(article, wrapper);
        return update?R.ok():R.error();
    }
}

